package com.example.parallelandroid;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.parallelandroid.entity.Suit;
import com.example.parallelandroid.entity.Weapon;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class UnlockFragment extends Fragment {
    //刷新按钮
    private Button btnReset;
    //点击穿戴或者解锁按钮 弹出来的对话框
    private UnlockDialog unlockDialog;
    //武器三个数据源的三个适配器
    private WeaponAdapter unlockedWeaponAdapter,unlockingWeaponAdapter,lockedWeaponAdapter;
    //服装三个数据源的三个适配器
    private SuitAdapter unlockedSuitAdapter,unlockingSuitAdapter,lockedSuitAdapter;
    //三个gridview
    private GridView gvUnlocked,gvUnlocking,gvLocked;
    //武器的三个数据源
    private List<Weapon> unlockedWeapons=new ArrayList<>();
    private List<Weapon> unlockingWeapons=new ArrayList<>();
    private List<Weapon> lockedWeapons=new ArrayList<>();
    //服装的三个数据源
    private List<Suit> unlockedSuits=new ArrayList<>();
    private List<Suit> unlockingSuits=new ArrayList<>();
    private List<Suit> lockedSuits=new ArrayList<>();
    private TextView tvUnlocking,tvLocked;
    private Spinner spinner;
    private String spName;
    //刷新页面用的两个常量
    private final int WEAPON=1;
    private final int SUIT=2;
    private SharedPreferences sharedPreferences;
    private String userId;
    //设置一个Handler用于刷新页面
    private Handler myHandler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            //处理子线程发送的消息
            switch (msg.what){
                case WEAPON:
                    //刷新GridView
                    unlockedWeaponAdapter = new WeaponAdapter(getContext(),R.layout.weapon_item_main,unlockedWeapons);
                    unlockingWeaponAdapter=new WeaponAdapter(getContext(),R.layout.weapon_item_main,unlockingWeapons);
                    lockedWeaponAdapter=new WeaponAdapter(getContext(),R.layout.weapon_item_main,lockedWeapons);
                    //绑定适配器
                    gvUnlocked.setAdapter(unlockedWeaponAdapter);
                    gvUnlocking.setAdapter(unlockingWeaponAdapter);
                    gvLocked.setAdapter(lockedWeaponAdapter);
                    setWeaponTvText();
                    break;
                case SUIT:
                    //刷新GridView
                    unlockedSuitAdapter=new SuitAdapter(getContext(),R.layout.suit_item_main,unlockedSuits);
                    unlockingSuitAdapter=new SuitAdapter(getContext(),R.layout.suit_item_main,unlockingSuits);
                    lockedSuitAdapter=new SuitAdapter(getContext(),R.layout.suit_item_main,lockedSuits);
                    //绑定适配器
                    gvUnlocked.setAdapter(unlockedSuitAdapter);
                    gvUnlocking.setAdapter(unlockingSuitAdapter);
                    gvLocked.setAdapter(lockedSuitAdapter);
                    setSuitTvText();
                    break;
            }
        }
    };
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // 获取布局文件
        View view = inflater.inflate(R.layout.fragment_unlock, null);
        //获取控件对象
        //已解锁的Gridview
        gvUnlocked = view.findViewById(R.id.gv_unlockeditem);
        //可解锁的Gridview
        gvUnlocking = view.findViewById(R.id.gv_unlockingitem);
        //未解锁的Gridview
        gvLocked = view.findViewById(R.id.gv_lockeditem);
        //获取可解锁和不可解锁有可能出现的文字控件
        tvUnlocking = view.findViewById(R.id.tv_unlocking);
        tvLocked = view.findViewById(R.id.tv_locked);
        //获取下拉框
        spinner =view.findViewById(R.id.spinner_class);
        //获取刷新按钮
        btnReset=view.findViewById(R.id.btn_reset);
        //设置下拉框的监听事件和刷新按钮的点击事件
        initSetting();
        sharedPreferences= getContext().getSharedPreferences("user", Context.MODE_PRIVATE);
        userId=sharedPreferences.getString("userId","");
        return view;
    }
    private void initSetting(){
        btnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (spinner.getSelectedItem().toString()){
                    case "武器":
                        getWeaponData();
                        break;
                    case "套装":
                        getSuitData();
                        break;
                }
            }
        });
        //选择下拉框变更下方内容
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                //一步获取下拉框的文本内容
                spName=spinner.getSelectedItem().toString();
                switch(spName){
                    case "武器":
                        getWeaponData();
                        //设置gridview的点击事件
                        setWeaponListener();
                        break;
                    case "套装":
                        getSuitData();
                        //设置gridview的点击事件
                        setSuitListener();
                        break;
                }

            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
    }
    //设置textview显示文字
    private void setWeaponTvText() {
        if (unlockingWeapons != null && unlockingWeapons.size() > 0){
            tvUnlocking.setText("");
        }else{
            tvUnlocking.setTextSize(20);
            tvUnlocking.setText("您当前没有可解锁的武器了");
        }

        if (lockedWeapons != null && lockedWeapons.size() > 0){
            tvLocked.setText("");
        }else{
            tvLocked.setTextSize(20);
            tvLocked.setText("您当前没有不可解锁的武器了");
        }
    }
    private void setSuitTvText(){
        if ((unlockingSuits != null && unlockingSuits.size() > 0)){
            tvUnlocking.setText("");
        }else{
            tvUnlocking.setTextSize(20);
            tvUnlocking.setText("您当前没有可解锁的套装了！");
        }
        if (lockedSuits != null && lockedSuits.size() > 0){
            tvLocked.setText("");
        }else{
            tvLocked.setTextSize(20);
            tvLocked.setText("您当前没有不可解锁的套装了");
        }
    }

    //服装界面 gridview的点击事件
    private void setSuitListener() {
        gvUnlocked.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Suit suit = unlockedSuits.get(position);
                //点击弹出dialog
                unlockDialog = new UnlockDialog(getContext(), R.style.UnlockDialog);
                //设置dialog的内容
                unlockDialog.setImg(suit.getSuitSrc());
                unlockDialog.setTitle(suit.getSuitName());
                unlockDialog.setTvRequire("智力:"+suit.getSuitRequireIntelligence()+"\n体力:"+suit.getSuitRequirePower());
                unlockDialog.setTvCharm(suit.getSuitCharm()+"");
                unlockDialog.setTvDes(suit.getSuitDescription());
                unlockDialog.setYesOnclickListener("穿戴", new UnlockDialog.onYesOnclickListener() {
                    @Override
                    public void onYesOnclick() {
                        wearSuit(suit.getSuitId());
                        unlockDialog.dismiss();
                    }
                });
                unlockDialog.show();
            }
        });
        gvUnlocking.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //获取当前对象
                Suit suit = unlockingSuits.get(position);
                //点击弹出dialog
                unlockDialog = new UnlockDialog(getContext(), R.style.UnlockDialog);
                //设置dialog的内容
                unlockDialog.setImg(suit.getSuitSrc());
                unlockDialog.setTitle(suit.getSuitName());
                unlockDialog.setTvRequire("智力:"+suit.getSuitRequireIntelligence()+"\n体力:"+suit.getSuitRequirePower());
                unlockDialog.setTvCharm(suit.getSuitCharm()+"");
                unlockDialog.setTvDes(suit.getSuitDescription());
                unlockDialog.setYesOnclickListener("解锁", new UnlockDialog.onYesOnclickListener() {
                    @Override
                    public void onYesOnclick() {
                        unlockSuit(suit.getSuitId());
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        unlockDialog.dismiss();
                        getSuitData();
                    }
                });
                unlockDialog.show();
            }
        });
        gvLocked.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Suit suit = lockedSuits.get(position);
                //点击弹出dialog
                unlockDialog = new UnlockDialog(getContext(), R.style.UnlockDialog);
                //设置dialog的内容
                unlockDialog.setImg(suit.getSuitSrc());
                unlockDialog.setTitle(suit.getSuitName());
                unlockDialog.setTvRequire("智力:"+suit.getSuitRequireIntelligence()+"\n体力:"+suit.getSuitRequirePower());
                unlockDialog.setTvCharm(suit.getSuitCharm()+"");
                unlockDialog.setTvDes(suit.getSuitDescription());
                unlockDialog.setYesOnclickListener("关闭", new UnlockDialog.onYesOnclickListener() {
                    @Override
                    public void onYesOnclick() {
                        //
                        unlockDialog.dismiss();
                    }
                });
                unlockDialog.show();
            }
        });
    }
    //武器界面 gridview的点击事件
    private void setWeaponListener() {
        gvUnlocked.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //获取当前对象
                Weapon weapon = unlockedWeapons.get(position);
                //点击弹出dialog
                unlockDialog = new UnlockDialog(getContext(), R.style.UnlockDialog);
                //设置dialog的内容
                unlockDialog.setImg(weapon.getWeaponSrc());
                unlockDialog.setTitle(weapon.getWeaponName());
                unlockDialog.setTvRequire("智力:"+weapon.getWeaponRequireIntelligence()+"\n体力:"+weapon.getWeaponRequirePower());
                unlockDialog.setTvCharm(weapon.getWeaponCharm()+"");
                unlockDialog.setTvDes(weapon.getWeaponDescription());
                unlockDialog.setYesOnclickListener("穿戴", new UnlockDialog.onYesOnclickListener() {
                    @Override
                    public void onYesOnclick() {
                        wearWeapon(weapon.getWeaponId());
                        unlockDialog.dismiss();
                    }
                });
                unlockDialog.show();
            }
        });
        gvUnlocking.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //获取当前对象
                Weapon weapon = unlockingWeapons.get(position);
                //点击弹出dialog
                unlockDialog = new UnlockDialog(getContext(), R.style.UnlockDialog);
                //设置dialog的内容
                unlockDialog.setImg(weapon.getWeaponSrc());
                unlockDialog.setTitle(weapon.getWeaponName());
                unlockDialog.setTvRequire("智力:"+weapon.getWeaponRequireIntelligence()+"\n体力:"+weapon.getWeaponRequirePower());
                unlockDialog.setTvCharm(weapon.getWeaponCharm()+"");
                unlockDialog.setTvDes(weapon.getWeaponDescription());
                unlockDialog.setYesOnclickListener("解锁", new UnlockDialog.onYesOnclickListener() {
                    @Override
                    public void onYesOnclick() {
                        unlockWeapon(weapon.getWeaponId());
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        getWeaponData();
                        unlockDialog.dismiss();
                    }
                });
                unlockDialog.show();
            }
        });
        gvLocked.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //获取当前对象
                Weapon weapon = lockedWeapons.get(position);
                //点击弹出dialog
                unlockDialog = new UnlockDialog(getContext(), R.style.UnlockDialog);
                //设置dialog的内容
                unlockDialog.setImg(weapon.getWeaponSrc());
                unlockDialog.setTitle(weapon.getWeaponName());
                unlockDialog.setTvRequire("智力:"+weapon.getWeaponRequireIntelligence()+"\n体力:"+weapon.getWeaponRequirePower());
                unlockDialog.setTvCharm(weapon.getWeaponCharm()+"");
                unlockDialog.setTvDes(weapon.getWeaponDescription());
                unlockDialog.setYesOnclickListener("关闭", new UnlockDialog.onYesOnclickListener() {
                    @Override
                    public void onYesOnclick() {
                        //
                        unlockDialog.dismiss();
                    }
                });
                unlockDialog.show();
            }
        });
    }
    //请求三种状态的武器的数据
    private void getWeaponData() {
        Constant.logger.info("从服务端获取三种状态的武器的数据 开始");
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(Constant.BASE_URL + "weapon/findAllWeapon?userId=" + userId)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Constant.logger.warn("从服务端获取三种状态的武器的数据: 失败", e);
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        // 此处执行UI操作
                        Toast.makeText(getContext(), "武器数据加载失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    //获取总的json
                    String dataJson = response.body().string();
                    //将总的json转成JSONObject
                    JSONObject jsonObject=new JSONObject(dataJson);
                    // 获取Result的msg
                    String msg = jsonObject.getString("msg");
                    Constant.logger.info("服务端Response: " + msg);
                    //获取data部分
                    JSONObject weaponArray=jsonObject.getJSONObject("data");
                    //拿到3个数据源
                    JSONArray unlockedWeaponArray=weaponArray.getJSONArray("unlocked");
                    JSONArray unlockingWeaponArray=weaponArray.getJSONArray("unlocking");
                    JSONArray lockedWeaponArray=weaponArray.getJSONArray("locked");
                    Gson gson = new Gson();
                    Type type = new TypeToken<List<Weapon>>(){}.getType();
                    unlockedWeapons=gson.fromJson(unlockedWeaponArray.toString(),type);
                    unlockingWeapons=gson.fromJson(unlockingWeaponArray.toString(),type);
                    lockedWeapons=gson.fromJson(lockedWeaponArray.toString(),type);
                    //数据源发送改变，需要更新界面，使用Handler
                    Message message = new Message();
                    message.what = WEAPON;
                    myHandler.sendMessage(message);//发送到主线程的消息队列
                } catch (JSONException e) {
                    Constant.logger.warn("解析服务端Response: 失败", e);
                }
            }
        });
    }
    //请求三种状态的服装的数据
    private void getSuitData() {
        Constant.logger.info("从服务端获取三种状态的服装的数据 开始");
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(Constant.BASE_URL + "suit/findAllSuit?userId=" + userId)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Constant.logger.warn("从服务端获取三种状态的服装的数据: 失败", e);
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        // 此处执行UI操作
                        Toast.makeText(getContext(), "加载失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    //获取总的json
                    String dataJson = response.body().string();
                    //将总的json转成JSONObject
                    JSONObject jsonObject=new JSONObject(dataJson);
                    // 获取Result的msg
                    String msg = jsonObject.getString("msg");
                    Constant.logger.info("服务端Response: " + msg);
                    //获取data部分
                    JSONObject suitArray=jsonObject.getJSONObject("data");
                    //拿到3个数据源
                    JSONArray unlockedSuitArray=suitArray.getJSONArray("unlocked");
                    JSONArray unlockingSuitArray=suitArray.getJSONArray("unlocking");
                    JSONArray lockedSuitArray=suitArray.getJSONArray("locked");
                    Gson gson = new Gson();
                    Type type = new TypeToken<List<Suit>>(){}.getType();
                    unlockedSuits=gson.fromJson(unlockedSuitArray.toString(),type);
                    unlockingSuits=gson.fromJson(unlockingSuitArray.toString(),type);
                    lockedSuits=gson.fromJson(lockedSuitArray.toString(),type);
                    //数据源发送改变，需要更新界面，使用Handler
                    Message message = new Message();
                    message.what = SUIT;
                    myHandler.sendMessage(message);//发送到主线程的消息队列
                } catch (JSONException e) {
                    Constant.logger.warn("解析服务端Response: 失败", e);
                }
            }
        });
    }
    //武器穿戴方法 给后端发送数据
    private void wearWeapon(Integer weaponId){
        Constant.logger.info("开始向服务端传输武器穿戴数据 weaponId = " + weaponId);
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(Constant.BASE_URL + "weapon/wearWeapon?userId=" + userId + "&weaponId=" + weaponId)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Constant.logger.warn("向服务端传输武器穿戴数据: 失败", e);
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        // 此处执行UI操作
                        Toast.makeText(getContext(), "穿戴失败 请稍后再试", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    String datasStr = response.body().string();
                    JSONObject jsonObject=new JSONObject(datasStr);
                    String code=jsonObject.getString("code");
                    // 获取Result的msg
                    String msg = jsonObject.getString("msg");
                    Constant.logger.info("服务端Response: " + msg);
                    if(code.equals("200")){
                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                // 此处执行UI操作
                                Toast.makeText(getContext(), "穿戴成功", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }else{
                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                // 此处执行UI操作
                                Toast.makeText(getContext(), "您已经穿戴上这件武器了", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                } catch (Exception e) {
                    Constant.logger.warn("解析服务端Response: 失败", e);
                }
            }
        });
    }
    //服装穿戴方法 给后端发送数据
    private void wearSuit(Integer suitId){
        Constant.logger.info("开始向服务端传输服装穿戴数据 suitId = " + suitId);
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(Constant.BASE_URL + "suit/wearSuit?userId=" + userId + "&suitId=" + suitId)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Constant.logger.warn("向服务端传输服装穿戴数据: 失败", e);
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        // 此处执行UI操作
                        Toast.makeText(getContext(), "穿戴失败 请稍后再试", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    String datasStr = response.body().string();
                    JSONObject jsonObject=new JSONObject(datasStr);
                    String code=jsonObject.getString("code");
                    // 获取Result的msg
                    String msg = jsonObject.getString("msg");
                    Constant.logger.info("服务端Response: " + msg);
                    if(code.equals("200")){
                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                // 此处执行UI操作
                                Toast.makeText(getContext(), "穿戴成功", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }else{
                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                // 此处执行UI操作
                                Toast.makeText(getContext(), "您已经穿戴上这件服装了", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                } catch (Exception e) {
                    Constant.logger.warn("解析服务端Response: 失败 okhttp onResponse", e);
                }
            }
        });
    }
    //武器解锁方法 给后端发送数据
    private void unlockWeapon(Integer weaponId){
        Constant.logger.info("开始向服务端传输武器解锁数据, weaponId = " + weaponId);
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(Constant.BASE_URL + "weapon/unlockWeapon?userId=" + userId + "&weaponId=" + weaponId)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Constant.logger.warn("向服务端传输武器解锁数据: 失败", e);
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        // 此处执行UI操作
                        Toast.makeText(getContext(), "解锁失败 请稍后再试", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    String datasStr = response.body().string();
                    JSONObject jsonObject=new JSONObject(datasStr);
                    // 获取Result的msg
                    String msg = jsonObject.getString("msg");
                    Constant.logger.info("服务端Response: " + msg);
                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                        @Override
                        public void run() {
                            // 此处执行UI操作
                            Toast.makeText(getContext(), "解锁成功", Toast.LENGTH_SHORT).show();
                        }
                    });
                } catch (Exception e) {
                    Constant.logger.warn("解析服务端Response: 失败", e);
                }
            }
        });
    }
    //服装解锁方法 给后端发送数据
    private void unlockSuit(Integer suitId){
        Constant.logger.info("开始向服务端传输服装解锁数据, suitId = " + suitId);
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(Constant.BASE_URL + "suit/unlockSuit?userId=" + userId + "&suitId=" + suitId)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Constant.logger.warn("向服务端传输服装解锁数据: 失败", e);
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        // 此处执行UI操作
                        Toast.makeText(getContext(), "解锁失败 请稍后再试", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    String datasStr = response.body().string();
                    JSONObject jsonObject=new JSONObject(datasStr);
                    // 获取Result的msg
                    String msg = jsonObject.getString("msg");
                    Constant.logger.info("服务端Response: " + msg);
                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                        @Override
                        public void run() {
                            // 此处执行UI操作
                            Toast.makeText(getContext(), "解锁成功", Toast.LENGTH_SHORT).show();
                        }
                    });
                } catch (Exception e) {
                    Constant.logger.warn("解析服务端Response: 失败", e);
                }
            }
        });
    }
}